/**
 * Created by Administrator on 2016/9/2.
 */

$(function () {


    //添加发送的openid
    $('.add-opemid').click(function () {
        var html = '';
        $('.add-opemid').hide();
        html += '<input type="text" name="openid" class="openInfo" placeholder="输入openid">';
        html += '<input type="text" name="remark" placeholder="输入备注">';
        html += '<a href="javascript:;" class="btn subOpenid">确认</a>';
        $('.Openid-Info').append(html);
    })


    //提交openid
    $('.Openid-Info').on('click', '.subOpenid', function () {
        var openid = $("input[name='openid']").val();
        if (openid == '') {
            standardTips('请填写openid');
            return false;
        }
        var remark = $("input[name='remark']").val();

        if (remark == '') {

            standardTips('请填写备注');
            return false;
        }

        $.post(saveOpenId, {openid: openid, remark: remark}, function (data) {
            if (data.errno == 2) {
                standardTips('已添加');
                return false;
            } else {
                var html = ' <span><input type="checkbox" class="checkbox-openid" data-remark="' + remark + '"  value="' + openid + '">' + openid + ' (' + remark + ')</span>';
                $('.control-list').append(html);
            }
        }, 'json');

        $("input[name='openid']").val('');
        $("input[name='remark']").val('');
    })


    //获取发送手机预览数据

    $('.activityList').on('click', '.setMobile', function () {

        var media_id = $(this).data('id');
        var title = $(this).data('title');
        var type = $(this).data('type');

        $("input[name='media_id']").val(media_id);
        $("input[name='media_title']").val(title);
        $("input[name='media_type']").val(type);
    })


    //提交手机预览
    $('.JsaveActivity').click(function () {

        var openidLength = $('.control-list').find("input[type='checkbox']:checked").length;

        console.log(openidLength);

        var openid = [];
        var remark = [];
        var media_id = $("input[name='media_id']").val();
        var type = $("input[name='media_type']").val();

        if (!openidLength) {
            var length = $('.Openid-Info').find('input').length;
            if (!length) {

                standardTips('请添加openid');
                return false;
            }

            var checkLength = $('.control-list').find('.checkbox-openid').length;

            if (!checkLength) {

                standardTips('请添加openid');
                return false;

            }

        }

        $('.control-list :checkbox').each(function () {
            if ($(this).is(":checked")) {
                openid.push($(this).val());
                remark.push($(this).data('remark'));
            }
        });

        console.log(openid);
        var data = {openid: openid, type: type, media_id: media_id, remark: remark};
        $.post(previewUrl, data, function (data) {
            if (data.errno == 1) {
                standardTips('发送成功');
                setTimeout(function () {
                    window.location.reload();
                }, 2000);
            } else {

                standardTips('已添加');

            }
        }, 'json')

    })


    var mateFlag = 1;
    //同步素材
    $('.sync-material').click(function () {
        if (!mateFlag) return !1;
        $.ajax({
            url: MaterialUrl,
            type: 'post',
            data: {data: 1},
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                $(".export-tags").html('<img src="' + RESOURCE_URL + '/images/loading.gif"/>');
            },
            success: function (data, textStatus) {
                if (data.errno == 0) {
                    standardTips(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000);
                } else {
                    standardTips(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000);
                }
            }
        })
        mateFlag = 0;
    })


    //推送范围
    $("input[name='tags']").change(function () {
        if ($(this).val() == 'all') {
            $('.tag-info').val($('.tag-text').text());
            $('ol li').find(':checkbox').prop('checked', false);
        } else {

            var tag = [];
            var city = [];
            $('ol .tag-name').each(function () {
                tag.push($(this).text());
            })

            $('ol .citylist').each(function () {
                city.push($(this).data('city'));
            })

            var cityid = city.join(',');
            var tagName = tag.join(',');
            console.log(cityid);
            $('.tag-info').val(tagName);
            $('.tag-info').attr('data-city', cityid);
            $('ol li').find(':checkbox').prop('checked', true);
        }
    })


    $('.push-tags').on('change', '.citylist', function (i, e) {

        $('.tag-info').attr('data-city', '');
        var city = [];
        var name = [];
        $('ol .citylist').each(function () {
            if ($(this).is(":checked")) {
                city.push($(this).data('city'));
                name.push($(this).parent('li').find('.tag-name').text());
            }
        })
        var cityid = city.join(',');
        var cityname = name.join(',');


        console.log(cityid);
        $('.tags-city').attr('checked', false);
        $('.tag-info').val(cityname);
        $('.tag-info').attr('data-city', cityid);

    })

    var preDefault = 1;
    //新建推送
    $('.checkSubmit').click(function () {
        standardConfirm('是否确认推送?',function(){
            if (!preDefault) return !1;
            var actid = $('.tag-info').data('actid');
            var tags = $('.tag-info').val();
            if (tags == '') {
                standardTips('请选择推送范围');
                return false;
            }
            var cityid = $('.tag-info').attr('data-city');
            var media_id = $('.mediaId').data('mediaid');
            var setTime = $("input[name='setMate']:checked").val();
            console.log(cityid);
            var data = {actid: actid, tags: tags, media_id: media_id, timeInfo: setTime, cityid: cityid};
            preDefault = 0;
            $.ajax({
                url: pushUrl,
                type: 'post',
                data: data,
                dataType: "json",
                beforeSend: function (XMLHttpRequest) {
                    $(".loadSubmit").html('<img src="' + RESOURCE_URL + '/images/loading.gif" />');
                },
                success: function (data, textStatus) {
                    if (data== 0) {
                        standardTips('发送成功');
                        window.location.href = pushList;
                    } else {
                        standardTips(data.msg);
                        location.reload();
                    }
                }
            })
        })
    })


    //离开次页面弹出提示框

    $('.act_main').on('click', 'a', function () {
        var url = $(this).data('url');
        standardConfirm('要离开此网页吗？', function () {
            location.href = url;
        })
    })

    $('.push-manger').on('click', 'a', function () {
        var url = $(this).data('url');
        standardConfirm('是否确认放弃该条推送？', function () {
            location.href = url;
        })
    })

    $('.table-list tr').each(function (i, e) {
        getWeixinImg($(e).find('img').attr('src'));
        $(e).each(function (n, m) {
            getWeixinImg($(m).find('img').attr('src'));
        })
    })
})

function getWeixinImg(url) {
    if (url) {
        var getUrl = url.split('?');
        return getUrl[0];
    }
}


function showImg(url) {
    var frameid = 'frameimg' + Math.random();
    console.debug(frameid);
    console.debug(url);
    window.img = '<img id="img" style="width:130px;height: 100px" src=\'' + url + '?' + Math.random() + '\' /><script>window.onload = function() { parent.document.getElementById(\'' + frameid + '\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script>';
    document.write('<iframe id="' + frameid + '" src="javascript:parent.img;" frameBorder="0" scrolling="no" width="130px" height="100px"></iframe>');
}
